package de.fhhof.vinf.ssp.ticketcenter.beans;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA {

	/**
	 * Variable to save the Password in the Bean after encoding
	 */
	private String encodedPassword = null;
	
	/**
	 * @return if set encoded Password else null
	 */
	public String getEncodedPassword(){
		return encodedPassword;
	}
	
	/**
	 * Method to generate a SHA Hash.
	 * This method also saves the encoded password in the encodePassword variable for further use,
	 * before the encoded String is returned.
	 * @param input String to encode
	 * @return encoded String
	 */
	public String getSHA(String input) {
		try {
			MessageDigest m = MessageDigest.getInstance("SHA");
			byte[] messageDigest = m.digest(input.getBytes());
			BigInteger number = new BigInteger(1, messageDigest);
			String hashtext = number.toString(16);
			while (hashtext.length() < 32) {
				hashtext = "0" + hashtext;
			}
			encodedPassword = hashtext;
			return encodedPassword;
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}
	}
}

